package online.githuboy.lagou.course.support;

import online.githuboy.lagou.course.utils.ReadTxt;
import online.githuboy.lagou.course.utils.StorageUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * @author eric
 * @program lagou-course-downloader
 * @description
 * @date 2021/8/23
 */
public class CourseHistory {

    private static volatile Set<String> historySet = new HashSet<>();

    /**
     * 记录已经下载过的视频id，不要重复下载了。
     */
    static String filePath = StorageUtil.getStotage() + "/course.txt";

    static {
        loadHistory();
    }

    /**
     * 下载完成之后追加到历史文件
     *
     * @param courseId
     */
    public static void append(String courseId) {
        historySet.add(courseId);
        new ReadTxt().writeFile(filePath, courseId);
    }

    public static Set<String> loadHistory() {
        Set<String> set = new ReadTxt().readFile(filePath);
        historySet.addAll(set);
        return historySet;
    }

    public static boolean contains(String id) {
        return historySet.contains(id);
    }

}
